//
//  ConnectPeopleViewController.m
//  tangshantongjiAPPFull
//
//  Created by yuntai on 2018/4/23.
//  Copyright © 2018年 yuntai. All rights reserved.
//

#import "ConnectPeopleViewController.h"
#import "FullMainModel.h"
#import "BMChineseSort.h"
#import "ContactChatListViewController.h"

@interface ConnectPeopleViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *array;

//排序后的出现过的拼音首字母数组
@property(nonatomic,strong)NSMutableArray *indexArray;
//排序好的结果数组
@property(nonatomic,strong)NSMutableArray *letterResultArr;

@end

@implementation ConnectPeopleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.array = [NSMutableArray array];
    self.indexArray = [NSMutableArray array];
    self.letterResultArr = [NSMutableArray array];
    
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.rowHeight = 55;
    BOOL ishow = self.array.count ? NO : YES;
    [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
    [self.tableview reloadData];

    weakself(self)
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakself.currentPage =1;
        [weakself getdata];
    }];
    [self.tableview.mj_header beginRefreshing];
    
    /**xmpp登录*/
    [self loginXmppServer];


}
#pragma mark tableviewdelegate datasource
//section的titleHeader
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [self.indexArray objectAtIndex:section];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.indexArray count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[self.letterResultArr objectAtIndex:section] count];
}
//section右侧index数组
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.indexArray;
}
//点击右侧索引表项时调用 索引与section的对应关系
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    return index;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *pricecellid = @"pricevideocellid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:pricecellid];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:pricecellid];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 55-0.7, kScreenBoundWidth, 0.7)];
        line.backgroundColor = KSepLineColor;
        [cell.contentView addSubview:line];
        cell.textLabel.textColor = kTextDeepDarkColor;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        
        UIImageView *yuanview = [[UIImageView alloc]initWithFrame:CGRectMake(15*UIRate, 10, 35, 35)];
        yuanview.layer.cornerRadius = 4;
        yuanview.layer.masksToBounds = YES;
        yuanview.tag = 100;
        [cell.contentView addSubview:yuanview];
        
        UILabel *namel = [[UILabel alloc]initWithFrame:CGRectMake(35+25*UIRate, 0, kScreenBoundWidth-100, 55)];
        namel.tag = 101;
        namel.textColor = kTextDeepDarkColor;
        namel.font = [UIFont boldSystemFontOfSize:14*UIRate];
        [cell.contentView addSubview:namel];
        
        
        
    }
    ContactMemModel *dic = [[self.letterResultArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    
    UILabel *namel = [cell.contentView viewWithTag:101];
    namel.text = dic.userName;
    
    UIImageView *imageview = [cell.contentView viewWithTag:100];
    imageview.image = [UIImage imageNamed:@"logo"];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //    NSDictionary *model = self.array[indexPath.section];
    ContactMemModel *model = [[self.letterResultArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

    ContactChatListViewController *vc = [[ContactChatListViewController alloc]init];
    vc.contactMemID = model.ContactId;
    vc.contactMemName  = model.userName;
    vc.title = model.userName?: @"聊天";
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)getdata{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSString *deptId = [UserAccountManager shareUserAccountManager].userModel.deptId;
    [params setValue:kTokenID forKey:@"token"];
    [params setValue:deptId?:@"" forKey:@"deptId"];
    
    [NetWork PostNetWorkWithUrl:@"api/contactsPeople/load.ajax" with:params successBlock:^(NSDictionary *dic) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        
        NSArray *temparray = dic[@"data"][@"result"];
        if (self.currentPage == 1) {
            [self.array removeAllObjects];
        }
        if (temparray.count<20) {
            [self.tableview.mj_footer endRefreshingWithNoMoreData];
        }else{
            self.currentPage ++;
            [self.tableview.mj_footer resetNoMoreData];
        }
        
        [self.array addObjectsFromArray:[ContactMemModel mj_objectArrayWithKeyValuesArray:temparray]];
        
        self.indexArray = [BMChineseSort IndexWithArray:self.array Key:@"userName"];
        self.letterResultArr = [BMChineseSort sortObjectArray:self.array Key:@"userName"];
        
        
        BOOL ishow = self.array.count ? NO : YES;
        [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
        [self.tableview reloadData];
        
    } FailureBlock:^(NSString *msg) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        [HUDManager showWarningWithText:msg];
        BOOL ishow = self.array.count ? NO : YES;
        [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
        
        
    } errorBlock:^(id error) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        BOOL ishow = self.array.count ? NO : YES;
        [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
        
        
    }];
    
}

@end
